<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('日志列表')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form action="" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">表信息</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="collectionName" placeholder="" type="text">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-filter="query" lay-submit>
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button class="pear-btn pear-btn-md" type="reset">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="table" lay-filter="table"></table>
    </div>
</div>
<script id="toolbar" type="text/html">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-add-1"></i>
        删除
    </button>
</script>

<script id="bar" type="text/html">
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"></i>
        删除
    </button>
</script>

</body>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'popup', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let popup = layui.popup;
        let common = layui.common;
        let MODULE_PATH = "/logDatabase/";
        let cols = [
            [
                {type: 'checkbox'},
                {field: 'destination', minWidth: 150, title: '实例名'},
                {field: 'databaseName', minWidth: 150, title: '数据库名称'},
                {field: 'tableName', minWidth: 100, title: '表名称'},
                {field: 'yearOfMonth', minWidth: 120, title: '年月'},
                {field: 'createDate', minWidth: 80, title: '创建时间'},
                {title: '操作', templet: '#bar', width: 250, align: 'center'}
            ]
        ]

        table.render({
            elem: '#table',
            url: MODULE_PATH + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            height: 'full-148',
            toolbar: '#toolbar',
            text: {none: '暂未创建信息'},
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
                title: "刷新"
            }, 'filter']
        });


        table.on('tool(table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            }
        });

        table.on('toolbar(table)', function (obj) {
            if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(query)', function (data) {
            window.refresh(data.field);
            return false;
        });

        window.batchRemove = function (obj) {
            let ids = common.checkField(obj, 'id');
            if (common.isEmpty(ids)) {
                popup.warning("未选中数据");
                return false;
            }
            layer.confirm('确定要删除选中表记录', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "batchRemove/" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            popup.success(result.msg, function () {
                                table.reload('table');
                            })
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        };

        window.remove = function (obj) {
            layer.confirm('确定要删除该记录', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            popup.success(result.msg, function () {
                                obj.del();
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }


        window.refresh = function (param) {
            table.reload('table', {where: param});
        }
        })
</script>
</html>